<?php

namespace app\common\model\mycurrency;

use think\Exception;
use think\Model;

class AgentMoneyWithdrawal extends Model
{
    // 表名
    protected $name = 'mycurrency_agent_money_withdrawal';

    // 追加属性
    protected $append = [
        'status_text',
        'payment_mode_text',
    ];

    const STATUS_QUXIAO = -1;//状态 = 取消
    const STATUS_BOHUI = -2;//状态 = 提现驳回
    const STATUS_DAICHULI = 1;//状态 = 待处理
    const STATUS_SHENHETONGGUO = 2;//状态 = 审核通过待打款
    const STATUS_DAKUANZHONG = 3;//状态 = 打款中
    const STATUS_YIDAKUAN = 4;//状态 = 已打款

    const PAYMENT_MODE_WEIXIN = 1;//体现方式 = 微信打款
    const PAYMENT_MODE_ZHIFUBAO = 2;//体现方式 = 支付宝打款
    const PAYMENT_MODE_XIANXIA = 3;//体现方式 = 线下打款
    const PAYMENT_MODE_WEIXINSHOUKUANMA     = 4;//提现方式:4=微信收款码收款
    const PAYMENT_MODE_ZHIFUBAOSHOUKUANMA   = 5;//提现方式:5=支付宝收款码收款

    /**
     * 代理商or业务员申请提现
     * @param int $agent_id 代理商or业务员id
     * @param int $money 提现金额
     * @param int $payment_mode 提现打款方式 1=微信打款,2=支付宝打款,3=银行卡线下打款,4=微信收款码收款,5=支付宝收款码收款
     * @param array $receive 收款信息 银行卡打款【卡号，开户行，开户人】 微信收款码打款【微信收款码图片地址】 支付宝收款码打款【支付宝收款码地址】
     * @param string $explain 体现说明
     */
    public static function withdrawal($agent_id,$money,$payment_mode,$receive = [],$explain = null){
        $agent_config = AgentConfig::where(['id' => 1])->field('shareratio,witserpro,minwit_amount')->find();
        if($money < $agent_config['minwit_amount']){
            throw new Exception("体现金额不得小于:{$agent_config['minwit_amount']}元");
            return false;
        }
        $agent = \app\common\model\mycurrency\Agent::where(['id' => $agent_id])->find();//->lock(true)
        if($agent->money < $money){
            throw new Exception("余额不足");
            return false;
        }
        $service_charge = number_format(( $money / 100 ) * $agent_config['witserpro'],2,'.','') ;//手续费
        $actual_payment = $money - $service_charge;//实际需要打款金额
        $increase = [
            'agent_id' => $agent['id'],
            'money' => $money,
            'witserpro' => $agent_config['witserpro'],
            'service_charge' => $service_charge,
            'actual_payment' => $actual_payment,
            'payment_mode' => $payment_mode,
            'explain' => $explain,
            'status' => self::STATUS_DAICHULI,
            'applytime' => time(),
            'createtime' => time(),
        ];
        $increase = array_merge($increase,$receive);
        $withdrawal = self::create($increase);
        /**
         * 处理余额变动
         * @param int $agent_id 代理商ID
         * @param int $money 变动金额
         * @param  int $type 变动类型
         * @param array $data 数据
         */
        AgentMoneylog::moneyChange($agent['id'],$money,AgentMoneylog::TYPE_TIXIAN,[
            'memo' => '代理商申请提现',
            'withdrawal_id' => $withdrawal->id,
        ]);
        return true;
    }

    public function getApplytimeAttr($value){
        return $value != null ? date('Y-m-d H:i:s', $value) : '';
    }

    public function getRejecttimeAttr($value){
        return $value != null ? date('Y-m-d H:i:s', $value) : '';
    }

    public function getToexaminttimeAttr($value){
        return $value != null ? date('Y-m-d H:i:s', $value) : '';
    }

    public function getPaymenttimeAttr($value){
        return $value != null ? date('Y-m-d H:i:s', $value) : '';
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = [
            '-1' => __('已取消'),
            '-2' => __('提现驳回'),
            '1' => __('待处理'),
            '2' => __('审核通过待打款'),
            '3' => __('打款中'),
            '4' => __('已打款'),
        ];
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getPaymentModeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['payment_mode']) ? $data['payment_mode'] : '');
        $list = [
            '1' => __('微信打款'),
            '2' => __('支付宝打款'),
            '3' => __('线下打款'),
        ];
        return isset($list[$value]) ? $list[$value] : '';
    }




    
}
